import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.onPointerEvent
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import components.SplitPanel
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import toolCategories.CodeAssistantToolCategory
import toolCategories.ToolCategory
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.Res
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.app_name
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.code


fun main() = application {
    Window(
        title = stringResource(Res.string.app_name),
        icon = painterResource(Res.drawable.code),
        state = rememberWindowState(width = 1000.dp, height = 1000.dp),
        onCloseRequest = ::exitApplication,
    ) {
        App()
    }
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
@Preview
fun App() {
    MaterialTheme {
        var currentTool by remember { mutableStateOf<ToolCategory>(CodeAssistantToolCategory) }
        SplitPanel(
            leftMinSize = 230.dp,
            defaultSplitPosition = 0.3f,
            left = {
                SideBar(selected = currentTool) { tool ->
                    currentTool = tool
                }
            },
            right = { MainContent(tool = currentTool) },
            splitter = {
                var indicatorAlpha by remember { mutableStateOf(0f) }
                Box(
                    modifier = Modifier
                        .alpha(indicatorAlpha)
                        .background(color = Color.Gray, shape = RoundedCornerShape(4.dp))
                        .width(2.dp)
                        .fillMaxHeight()
                        .padding(vertical = 4.dp)
                        .onPointerEvent(PointerEventType.Enter) {
                            indicatorAlpha = 1.0f
                        }
                        .onPointerEvent(PointerEventType.Exit) {
                            indicatorAlpha = 0f
                        }
                )
            }
        )
    }
}
